﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>测试</title>
    <script src="js/jquery.js" type="text/javascript"></script>
    <script src="js/jquery.mobile-1.4.2.js" type="text/javascript"></script>
    <link href="css/themes/default/jquery.mobile-1.4.2.min.css" rel="stylesheet" type="text/css" />
    <script src="javescript/test.js" type="text/javascript"></script>
</head>
<body>
    <input type="button" id="test_click" value="测试点击" />
    <script src="js/cordova.js" type="text/javascript"></script>
    <script type="text/javascript">

        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            alert("onDeviceReady");
            document.addEventListener("menubutton", onMenuKeyDown, false);
            document.addEventListener("backbutton", yourCallbackFunction, false);
            //openDB();
            var db = window.openDatabase("sugarDB", "1.0", "sugar", 1000000);
            db.transaction(populateDB, errorCB, successCB);
        }
        function onMenuKeyDown() { alert("menubutton"); }
        function onBackKeyDown() { alert("backbutton"); }


        $("#test_click").click(function () {
            alert("测试点击");
        });

        //数据库
        function openDB() {
            alert("创建数据库001");
            var db = window.openDatabase("sugarDB", "1.0", "sugar", 1000000);
            db.transaction(populateDB, errorCB, successCB);
        }
        function populateDB(tx) {
            //创建数据登录用户信息表
            var CreateDataTableUserSql = "create table if not exists LoginInfo (android integer not null primary key,id integer,loginName text,password text,details text)";
            tx.executeSql(CreateDataTableUserSql);
            //插入初始数据
            var InsertDataTableUserSql = "insert into  LoginInfo (id,loginName,password,details) values(1,'admin','admin','')";
            tx.executeSql(InsertDataTableUserSql);
        }
        function errorCB(err) {
            console.log("错误处理的SQL：" + err.code + "错误信息：" + err.message);
            alert("错误处理的SQL：" + err.code + "错误信息：" + err.message);
        }

        // 事务执行成功后调用的回调函数
        function successCB() {
            //  var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
            //  db.transaction(queryDB, errorCB);
            console.log("成功！");
            alert("OK");
        }
       
    </script>
    <input type="button" value="查询数据SQL" id="btn_Select" />
    <script type="text/javascript">
        $("#btn_Select").click(function () {
            var db = window.openDatabase("sugarDB", "1.0", "sugar", 1000000);
            db.transaction(queryDB, errorCB, successCB);
        });
        // 查询数据库
        function queryDB(tx) {
            var selectsql = "select * from LoginInfo";
            tx.executeSql(selectsql, [], querySuccess, errorCB);
        }
        // 查询成功后调用的回调函数
        function querySuccess(tx, results) {
            if (results.rows.length != 0) {
                var id = results.rows.item(0).id
                var loginName = results.rows.item(0).loginName
                alert("查询OK" + id + "登录名" + loginName);
            }
            // 因为没有插入记录，所以返回值为空
            //            console.log("Insert ID = " + results.insertId);
            //            // 因为这是一条查询语句所以返回值为0
            //            console.log("Rows Affected = " + results.rowAffected);
            //            // 返回查询到的记录行数量
            //            console.log("Insert ID = " + results.rows.length);
        }
        function errorCB(err) {
            console.log("错误处理的SQL：" + err.code + "错误信息：" + err.message);
        }
        // 事务执行成功后调用的回调函数
        function successCB() {
            alert("查询数据执行成功");
        }
    </script>
</body>
</html>
